﻿using IServices;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using YL.Core.Dto;
using YL.Core.Entity;
using YL.Core.Entity.Fluent.Validation;
using YL.Core.Entity.Meter;
using YL.NetCore.Attributes;
using YL.NetCore.NetCoreApp;
using YL.Utils.Extensions;
using YL.Utils.Json;
using YL.Utils.Pub;
using YL.Utils.Table;

namespace Method.SmartMeter.Controllers
{
    public class CameraController : BaseController
    {
        private readonly IMeter_CameraServices _cameraServices;
        private readonly IMeter_AllLevelUserClientInfoViewServices _levelClientServices;
        private readonly IMeter_DeviceInfoServices _deviceServices;

        public CameraController(IMeter_CameraServices cameraServices, 
            IMeter_AllLevelUserClientInfoViewServices levelClientServices,
            IMeter_DeviceInfoServices deviceServices)
        {
            _cameraServices = cameraServices;
            _levelClientServices = levelClientServices;
            _deviceServices = deviceServices;
        }

        [HttpGet]
        [CheckMenu]
        public IActionResult Index()
        {
            return View();
        }


        [HttpPost]
        [OperationLog(LogType = LogType.select)]
        public ContentResult List([FromForm]Bootstrap.BootstrapParams bootstrap)
        {
            bootstrap.UserId= UserDtoCache.UserId;

            var datas = _cameraServices.PageList(bootstrap);
            return Content(datas);
        }

        [HttpPost]
        public IActionResult GetClientList()
        {
            List<AllLevelUserClientInfoView> datas = _levelClientServices.QueryableToList(s=>s.UserId== UserDtoCache.UserId);
            return Json(datas);
        }
        [HttpPost]
        public IActionResult GetDeviceList(string ClientId)
        {
            long id = Convert.ToInt64(ClientId);
            List<DeviceInfo> datas = _deviceServices.QueryableToList(s => s.ClientId == id);
            return Json(datas);
        }

        [HttpGet]
        public IActionResult Add(string id)
        {

            Camera camera = new Camera();

            if (id.IsEmpty())
            {
                List<Camera> lists = new List<Camera>();
                ViewBag.parentName = JsonConvert.SerializeObject(lists);
                return View(camera);
            }
            else
            {
                long CameraId = Convert.ToInt64(id);
                camera = _cameraServices.QueryableToEntity(c => c.CameraId == CameraId);
                return View(camera);
            }
        }

        [HttpPost]
        [FilterXss]
        [OperationLog(LogType = LogType.addOrUpdate)]
        public IActionResult AddOrUpdate([FromForm]Camera camera, [FromForm]string id)
        {
            if (id.IsEmptyZero())
            {
                if (_cameraServices.IsAny(c => c.CameraName == camera.CameraName && c.IsDel == 1))
                {
                    return BootJsonH((false, PubConst.Camera1));
                }
                camera.CameraId = PubId.SnowflakeId;
                camera.CreateBy = UserDtoCache.UserId;

                bool flag = _cameraServices.Insert(camera);
                return BootJsonH(flag ? (flag, PubConst.Add1) : (flag, PubConst.Add2));
            }
            else
            {
                camera.CameraId = id.ToInt64();
                camera.ModifiedBy = UserDtoCache.UserId;
                camera.ModifiedDate = DateTimeExt.DateTime;
                var flag = _cameraServices.Update(camera,null,false);
                return BootJsonH(flag ? (flag, PubConst.Update1) : (flag, PubConst.Update2));
            }
        }

        [HttpGet]
        [OperationLog(LogType.delete)]
        public IActionResult Delete(string id)
        {
            var flag = _cameraServices.Update(new Camera { CameraId = SqlFunc.ToInt64(id), IsDel = 0, ModifiedBy = UserDtoCache.UserId, ModifiedDate = DateTimeExt.DateTime }, c => new { c.IsDel, c.ModifiedBy, c.ModifiedDate });
            return BootJsonH(flag ? (flag, PubConst.Delete1) : (flag, PubConst.Delete2));
        }
    }
}